package com.csdn.springbootmybatis.service.impl;

import com.csdn.springbootmybatis.mapper.RoleMapper;
import com.csdn.springbootmybatis.mode.Role;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: JLL
 * @Date: 2019/8/24 15:52
 */
@Service
public class RoleService {


    @Resource
    RoleMapper roleMapper;

    public PageInfo<Role> findByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> roleList = roleMapper.selectAll();
        return new PageInfo<>(roleList);
    }

    public Role findById(int id) {
        return roleMapper.findById(id);
    }

    @Transactional
    public void addPermission(int id, int[] permissions) {
        //这里用for，也可以在sql xml里，用批量提交。
        //for循环会发多次sql，影响性能。  如果只发一个sql，就会好很多。
		/*for (int p : permissions) {

			roleMapper.addPermission(id,p);
		}*/
        roleMapper.addPermissions(id,permissions);
    }
}
